<?php


function mbStringToArray ($string) {
    $strlen = mb_strlen($string);
    while ($strlen) {
        $array[] = mb_substr($string,0,1,"UTF-8");
        $string = mb_substr($string,1,$strlen,"UTF-8");
        $strlen = mb_strlen($string);
    }
    return $array;
} 

$oldalpha = array(
"1"=>"أ",
"2"=>"ب",
"3"=>"ج",
"4"=>"د",
"5"=>"ه",
"6"=>"و",
"7"=>"ز",
"8"=>"ح",
"9"=>"ط",
"10"=>"ي",
"11"=>"ك",
"12"=>"ل",
"13"=>"م",
"14"=>"ن",
"15"=>"س",
"16"=>"ع",
"17"=>"ف",
"18"=>"ص",
"19"=>"ق",
"20"=>"ر",
"21"=>"ش",
"22"=>"ت",
"23"=>"ث",
"24"=>"خ",
"25"=>"ذ",
"26"=>"ض",
"27"=>"ظ",
"28"=>"غ"



);
$newalpha = array(

"1"=>"أ",
"2"=>"ب",
"3"=>"ت",
"4"=>"ث",
"5"=>"ج",
"6"=>"ح",
"7"=>"خ",
"8"=>"د",
"9"=>"ذ",
"10"=>"ر",
"11"=>"ز",
"12"=>"س",
"13"=>"ش",
"14"=>"ص",
"15"=>"ض",
"16"=>"ط",
"17"=>"ظ",
"18"=>"ع",
"19"=>"غ",
"20"=>"ف",
"21"=>"ق",
"22"=>"ك",
"23"=>"ل",
"24"=>"م",
"25"=>"ن",
"26"=>"ه",
"27"=>"و",
"28"=>"ي"


);

function getalphanum($a,$newold) {
switch ($a) {


case "ء":
    $z=1;
    $n=1;
    break;
case "ئ":
    $z=1;
    $n=1;
    break;
case "آ":
    $z=1;
    $n=1;
    break;
case "إ":
    $z=1;
    $n=1;
    break;
case "ا":
    $z=1;
    $n=1;
    break;
case "ؤ":
    $z=1;
    $n=1;
    break;
case "ي":
    $z=10;
    $n=28;
    break;
case "ة":
    $z=22;
    $n=3;
    break;
case "أ":
    $z=1;
    $n=1;
    break;
  case "ب":
    $z=2;
    $n=2;
    break;
  case "ج":
    $z=3;
    $n=5;
    break;
case "د":
    $z=4;
    $n=8;
    break;
  case "ه":
    $z=5;
    $n=26;
    break;
case "و":
    $z=6;
    $n=27;
    break;
  case "ز":
    $z=7;
    $n=11;
    break;
  case "ح":
    $z=8;
    $n=6;
    break;
case "ط":
    $z=9;
    $n=16;
    break;

 case "ى":
    $z=10;
    $n=28;
    break;

case "ك":
    $z=11;
    $n=22;
    break;
  case "ل":
    $z=12;
    $n=23;
    break;
case "م":
    $z=13;
    $n=24;
    break;
  case "ن":
    $z=14;
    $n=25;
    break;
case "س":
    $z=15;
    $n=12;
    break;
  case "ع":
    $z=16;
    $n=18;
    break;
case "ف":
    $z=17;
    $n=20;
    break;
  case "ص":
    $z=18;
    $n=14;
    break;
case "ق":
    $z=19;
    $n=21;
    break;
  case "ر":
    $z=20;
    $n=10;
    break;
case "ش":
    $z=21;
    $n=13;
    break;
case "ت":
    $z=22;
    $n=3;
    break;
case "ث":
    $z=23;
    $n=4;
    break;
  case "خ":
    $z=24;
    $n=7;
    break;
case "ذ":
    $z=25;
    $n=9;
    break;
  case "ض":
    $z=26;
    $n=15;
    break;
case "ظ":
    $z=27;
    $n=17;
    break;
  case "غ":
    $z=28;
    $n=19;
    break;
case " ":
    $z=0;
    $n=0;
    break;
  default:
    $z=-10000000;
    $n=-10000000;
}
  if ($newold==1) return $z;
  if ($newold==2) return $n;
}


echo "<style>table, th, td {    border: 1px solid black;}</style><table border='1' style='width:100%'>";
$oldnew=1; // 1 old 2 new
//$con=mysqli_connect("localhost","quran","quran","Quran-Simple");
$con=mysqli_connect("localhost","quran","quran","Quran");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"select sura,aya,text from quran_text2 where sura=2 and aya> 1 and aya<15");
$stposnospacecont=0;
while($row = mysqli_fetch_array($result)) 
{
echo "<tr><td width='100%'>";

  echo "<strong>(".$row['aya'] . ") " . $row['text']."</strong><br>";
$total=0;
$totalbig=0;
$stpos=0;
$last=0;
$totdiff=0;
$diff=0;
$stposnospace=0;
$totalalm=0;
echo "</td></tr>";

echo "<table border='1' style='width:100%'>";
	foreach (mbStringToArray($row['text']) as $ch)
		{
		echo "<td>";
				
		$stpos=$stpos+1;
		
		$s=getalphanum($ch,$oldnew);
		if ($s<>0) $stposnospace=$stposnospace+1;
		if ($s<>0) $stposnospacecon=$stposnospacecont+1;
			if ( $stposnospace%3== 1) $adds=getalphanum("ا",$oldnew);
			if ( $stposnospace%3== 2) $adds=getalphanum("ل",$oldnew);
			if ( $stposnospace%3== 0) $adds=getalphanum("م",$oldnew);
	
		if ($last!==0  ) {
			$diff=$s-$last;
			if ( $stpos%2== 1) $totdiff=$diff-$totdiff;
			if ( $stpos%2== 0) $totdiff=$diff+$totdiff;
			//$totdiff=abs($diff)+$totdiff; 
			}		
		if ($s<>0) $last=$s;		
		$total=$total+$s;
		if ($s<>0) $totalalm=$totalalm+$s+$adds;
		if ($s<>0) $totalbig=$totalbig+($stpos*$s);
		echo ("".$ch."<br> ".getalphanum($ch,$oldnew)."<br>diff:".$diff."<br>tdiff:".$totdiff."<br>".$stposnospace."<br>".$adds."<br>".$oldalpha[$s+$adds]);
echo "</td>";
		}
echo "</table>";

echo "<tr><td>";
echo "Total:".$total."<br>";
$re=$total%28;
$rebig=$totalbig%28;
$retotalalm=$totalalm%28;
echo "Mod:".$re."<br>";
echo "totalbig:".$totalbig."<br>Modbig:".$rebig."<br>";
echo "totdiff:".$totdiff."<br>";
echo "totalm:".$totalalm."<br>";
echo "modtotalalm:".$retotalalm."<br>";
echo "</td></tr>";
}
echo "</table>";
mysqli_close($con);
?> 



